<?php
namespace api\home\controller;
use app\common\model\BanjiModel;
use app\common\model\VideoModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Request;

class HomeController extends RestBaseController
{
    public function index(){

        $info = Db::name('video')->find();
        $url = cmf_get_image_url($info['url']);

        $data = $this->request->param();
        $url = cmf_get_image_url('/upload/20220516/3bda8117c9ed67132a343424aedbd3d2.jpg');

        $this->success('请求成功!', ['test' => 'test', 'data' => $data]);
    }

    public function upload(Request $request){
        //获取文件
        $file = $request->file('file');
        $data = $this->request->param();

        if(empty($file)){
            $this->error('请选择上传文件');
        }
        //移动到框架应用根目录/public/uploads/
        $info = $file->validate(['ext' => 'mp3'])->move(CMF_ROOT.'public/upload/video');
//        $info = $file->validate(['ext' => 'mp3'])->move(CMF_ROOT.'public/uploads/');
        if($info){
            //获取上传文件的url
            $image_url   = '/upload/video/'.str_replace("\\","/",$info->getSaveName());
            $data['url'] = $image_url;
            $data['create_time'] = time();
            $data['update_time'] = time();
            VideoModel::insert($data);
            $this->success('文件上传成功');
        }else{
            //上传失败获取错误信息
            $this->error($file->getError());
        }
    }


    public function getClass(){

        $list = BanjiModel::field('id,name')->select();
        $this->success('请求成功!', [ 'banji' => $list]);
    }
}